#!/bin/sh

op=$1

os=`uname -s`
awkc=awk
if [ "$os" = "SunOS" ]; then
   awkc=nawk
fi

orauser=${LB_ORACLE_USER}
if [ -z "$orauser" ]; then
    orauser=`ps -ef | grep ora_pmon | grep -v "grep" | head -n1 | sed -e 's/^[ \t]*//' | cut -d" " -f1`
fi
if [ -z "$orauser" ];then
    orauser=oracle
fi

user_id=`id ${orauser}`
if [ -z "$user_id" ];then
    echo "can't find user: ${orauser}"
    exit 1
fi

oraclehome=${LB_ORACLE_HOME}
if [ -z "$oraclehome" ];then
    oraclehome=`cat /etc/oratab | grep -v "^#" | grep -v "^\+" | grep ".*:.*:[Y:N]" | head -n1 | cut -d":" -f2`
fi
if [ -z "$oraclehome" ];then
    oraclehome=`su - oracle -c "echo \$ORACLE_HOME"`
fi
if [ -z "$oraclehome" ];then
    oraclehome=`su - $orauser -c "echo \$ORACLE_HOME"`
fi

listener=${oraclehome}/network/admin

group=`id -gn ${orauser}`

scriptsRoot=${LB_ROOT}
if [ -z "$scriptsRoot" ]; then
  scriptsRoot=/opt/howlink/linkBackup/resource/scripts
  export LB_ROOT=${scriptsRoot}
fi

echo_help()
{
    echo "Usage: $0 {create osid sid <target> <rtime> rlog}"
    echo "       $0 {delete sid <target> <rtime> rlog}"
}

validate_sid()
{
    a=$1
    if [ -z "$a" ];then
        echo_help
        exit 1
    fi
}

validate_target()
{
    a=$1
    if [ -z "$a" ];then
        echo_help
        exit 1
    fi
}

validate_store()
{
    a=$1
    if [ -z "$a" ];then
        echo_help
        exit 1
    fi

    if [ ! -e "$a" ];then
        echo "$a is not exists"
        exit 1
    fi
}

validate_rtime()
{
    a=$1
    if [ -z "$a" ];then
        echo_help
        exit 1
    fi
}

validate_rlog()
{
    a=$1
    if [ -z "$a" ];then
        echo_help
        exit 1
    fi
}


create()
{
    osid=$1
    sid=$2
    target=$3
    store=$4
    rtime=$5
    rlog=$6
    params=$7

    if [ -e "$params" ];then
        chown -R ${orauser}:${group} ${params}
    fi

    chown -R ${orauser}:${group} ${store}
    chown -R ${orauser}:${group} ${scriptsRoot}/oracle/asmclone
    nonid=false
    notnsupdate=false

    rrecovery=true

    standalone=true

    tnsadmindir=${listener}
    isrestore=false
    isgrandchild=false
    isremount=false
    ischild=false
    opname=mount

    useasm=true

    hostip="localhost"
    LB_ROOT=${scriptsRoot}
    LB_PARAM=${params}

    export osid
    export sid
    export nonid
    export notnsupdate
    export orahome=${oraclehome}
    export rrecovery
    export standalone
    export tnsadmindir
    export username=${orauser}
    export isrestore
    export isgrandchild
    export isremount
    export hostip
    export ischild
    export opname
    export asm_base
    export useasm
    export LB_ROOT
    export LB_PARAM

    if [ ! -e "$store" ];then
        mkdir ${store}
    fi
    if [ ! -e "$rlog" ];then
        touch ${rlog}
    fi
    chown ${orauser}:${group} ${rlog}

    # ${scriptsRoot}/oracle/asmclone/OracleAppMount.sh $sid $orahome $group_name $hostip 2019-01-29..15:59:19
    ${scriptsRoot}/oracle/asmclone/OracleAppMount.sh ${sid} ${oraclehome} ${target} ${store} ${hostip} ${rtime} >> ${rlog} 2>> ${rlog}
}

delete()
{
    sid=$1
    target=$2
    store=$3
    rlog=$4

    if [ ! -e "$store" ];then
        mkdir ${store}
    fi
    if [ ! -e "$rlog" ];then
        touch ${rlog}
    fi
    chown ${orauser}:${group} ${rlog}

    ${scriptsRoot}/oracle/asmclone/OracleASMDBRewind_nonrac.sh ${sid} ${oraclehome} ${orauser} >> ${rlog} 2>> ${rlog}
}

case ${op} in
create)
    osid=$2
    sid=$3
    target=$4
    store=$5
    rtime=$6
    rlog=$7
    params=$8

    validate_sid ${osid}
    validate_sid ${sid}
    validate_target ${target}
    validate_store ${store}
    validate_rtime ${rtime}
    validate_rlog ${rlog}

    create ${osid} ${sid} ${target} ${store} ${rtime} ${rlog} ${params}
    ;;
delete)
    sid=$2
    target=$3
    store=$4
    rlog=$5

    validate_sid ${sid}
    validate_target ${target}
    validate_store ${store}
    validate_rlog ${rlog}

    delete ${sid} ${target} ${store} ${rlog}
    cat /etc/oratab | sed "/${sid}:.*:Y/d" > /etc/oratab
    ;;
*)
    echo_help
    exit 1
esac